package org.grill.clovercash;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.grill.clovercash.accounts.Account;
import org.grill.clovercash.accounts.Account.Type;

public class AccountEditDialog extends Dialog {
    public AccountEditDialog(Shell parentShell) {
        super(parentShell);
    }
	
    Text nameText;
    Combo typeCombo;
    
    private String name = "New Account";
    private String description = "Description";
    private Account.Type type = Type.ASSET;

	private Text descriptionText;
    
    public void setName(String name) {
    	this.name = name;
    }
    
    public String getName() {
    	return name;
    }
    
    public void setDescription(String description) {
    	this.description = description;
    }
    
    public String getDescription() {
    	return description;
    }
    
	protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        GridLayout layout = (GridLayout) container.getLayout();
        layout.numColumns = 2;

        new Label(container, SWT.LEFT).setText("Account Name:");
        nameText = new Text(container, SWT.BORDER);
        nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        nameText.setText(name);
        
        new Label(container, SWT.LEFT).setText("Description:");
        descriptionText = new Text(container, SWT.BORDER);
        descriptionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        descriptionText.setText(description);

        new Label(container, SWT.LEFT).setText("Type:");
        typeCombo = new Combo(container, SWT.READ_ONLY);
        typeCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        
        for (Type t : Type.values())
        	typeCombo.add(t.toString());
        typeCombo.setText(type.toString());
        
        return container;
    }

	protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID,
            IDialogConstants.OK_LABEL, true);
        createButton(parent, IDialogConstants.CANCEL_ID,
            IDialogConstants.CANCEL_LABEL, false);
    }
    
    protected Point getInitialSize() {
        return new Point(500, 375);
    }

	@Override
	protected void okPressed() {
		name = nameText.getText();
		description = descriptionText.getText();
		type = Type.getType(typeCombo.getText());
		
		super.okPressed();
	}

	public Account.Type getType() {
		return type;
	}

	public void setType(Account.Type type) {
		this.type = type;
	}
}
